package dormouse;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.Timer;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileType;
import org.jdesktop.application.Action;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.Task;
import org.jdesktop.application.TaskMonitor;
import org.jdesktop.swingx.JXErrorPane;
import org.jdesktop.swingx.combobox.ListComboBoxModel;

import dormouse.vfs.event.IFileSelectionEvent;
import dormouse.vfs.event.IFileSelectionListener;
import dormouse.vfs.metadata.PreviewPanel;
import dormouse.vfs.ui.FileNode;
import dormouse.vfs.ui.FileTree;
import dormouse.vfs.ui.SelectableFile;
import dormouse.vfs.ui.actions.BackAction;
import dormouse.vfs.ui.actions.ReloadAction;
import dormouse.vfs.ui.actions.ShowBookmarksTask;
import dormouse.vfs.ui.actions.ShowContactsTask;
import dormouse.vfs.ui.actions.ShowDocsTask;
import dormouse.vfs.ui.actions.ShowHomeTask;
import dormouse.vfs.ui.actions.ShowMusicTask;
import dormouse.vfs.ui.actions.ShowPicturesTask;
import dormouse.vfs.ui.actions.ShowVideoTask;
import dormouse.vfs.ui.actions.UpAction;
import dormouse.vfs.ui.renderers.FileListRendererFactory;
import dormouse.vfs.ui.renderers.IFileListRenderer;

/**
 * The application's main frame.
 */
public class DormouseView extends FrameView {

    private FileTree fileTree = null;
    private PreviewPanel previewPanel = new PreviewPanel();

    /**
     * Constructor
     * 
     * @param app
     *            The application that the view resides in.
     */
    public DormouseView(SingleFrameApplication app) {
        super(app);

        initComponents();

        this.locationFld.setFileLocation(System.getProperty("user.home"));

        this.viewCombo.setModel(new ListComboBoxModel(FileListRendererFactory.getRenderers()));

        this.viewCombo.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                initializeRenderer();
            }
        });

        this.listRendererContainer.setLayout(new BorderLayout());

        // status bar initialization - message timeout, idle icon and busy
        // animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            public void propertyChange(
                    java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String) (evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? ""
                            : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer) (evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });

        ButtonGroup group = new ButtonGroup();
        group.add(this.bookmarksBtn);
        group.add(this.docsBtn);
        group.add(this.homeBtn);
        group.add(this.musicBtn);
        group.add(this.picturesBtn);
        group.add(this.videoBtn);

        this.fileTree = new FileTree();
        this.fileTree.addTreeSelectionListener(new TreeSelectionListener() {

            public void valueChanged(TreeSelectionEvent e) {
                Object comp = e.getPath().getLastPathComponent();
                FileObject dir = null;
                if (comp instanceof SelectableFile) {
                    SelectableFile node = (SelectableFile) comp;
                    dir = (FileObject) node.getFile();
                } else if (comp instanceof FileNode) {
                    FileNode node = (FileNode) comp;
                    dir = (FileObject) node.getFile();
                }

                DormouseView.this.locationFld.setFileLocation(dir.getName().getURI());

            }
        });

        // initialize the renderer.
        initializeRenderer();

        this.locationFld.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setLocation();

            }
        });

        this.locationFld.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                setLocation();
            }
        });

    }

    public FileObject getLocation() {
        FileObject locFile = this.locationFld.getFileLocation();
        return locFile;
    }

    private void initializeRenderer() {
        IFileListRenderer<Component> renderer = (IFileListRenderer<Component>) this.viewCombo.getSelectedItem();
        JPanel container = this.listRendererContainer;
        container.removeAll();
        container.add(renderer.getComponent(), BorderLayout.CENTER);
        container.validate();
        setLocation();
    }

    /**
     * This method is used by the locationFld's change listener to respond to
     * changes in the locationFld's value. This allows the view to respond to
     * manually entered changes in the locationFld, as well as programmatic
     * changes performed by clicking the buttons.
     */
    private void setLocation() {

        try {
            FileObject locFileObj = this.locationFld.getFileLocation();
            if (locFileObj != null) {
                FileObject dir = (locFileObj.getType() == FileType.FOLDER) ? locFileObj
                        : locFileObj.getParent();

                if (dir.exists()) {
                    IFileListRenderer renderer = (IFileListRenderer) this.viewCombo.getSelectedItem();
                    renderer.render(dir, null); //TODO: add predicate support
                } else {
                    throw new RuntimeException(
                            "The directory does not exist: " + dir.getName().toString());
                }
            }

        } catch (FileSystemException ex) {
            Logger.getLogger(DormouseView.class.getName()).log(
                    Level.SEVERE, null, ex);
            JXErrorPane.showDialog(ex);
        } catch (Throwable th) {
            JXErrorPane.showDialog(th);
        }

    }

    public void setLocation(FileObject dir) {
        this.locationFld.setFileLocation(dir);
    }

    public void setLocation(String location) {
        this.locationFld.setFileLocation(location);
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = Dormouse.getApplication().getMainFrame();
            aboutBox = new DormouseAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        Dormouse.getApplication().show(aboutBox);
    }

    @Action
    public void togglePreview() {
        JToggleButton src = DormouseView.this.previewBtn;
        if (src.isSelected()) {
            showPreview(true);
        } else {
            showPreview(false);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=
    // <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
    	
    	Font font = new Font(Font.DIALOG, Font.PLAIN, 10);

        mainPanel = new javax.swing.JPanel();
        jToolBar2 = new javax.swing.JToolBar();
        jLabel1 = new javax.swing.JLabel();
        locationFld = new dormouse.vfs.ui.LocationComboBox();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        searchFld = new javax.swing.JTextField();
        jToolBar1 = new javax.swing.JToolBar();
        backBtn = new javax.swing.JButton();
        backBtn.setBorder(BorderFactory.createEmptyBorder());
        backBtn.setFont(font);
        
        upBtn = new javax.swing.JButton();
        upBtn.setBorder(BorderFactory.createEmptyBorder());
        upBtn.setFont(font);
        
        refreshBtn = new javax.swing.JButton();
        refreshBtn.setBorder(BorderFactory.createEmptyBorder());
        refreshBtn.setFont(font);
        
        bookmarksBtn = new javax.swing.JToggleButton();
        homeBtn = new javax.swing.JToggleButton();
        docsBtn = new javax.swing.JToggleButton();
        picturesBtn = new javax.swing.JToggleButton();
        musicBtn = new javax.swing.JToggleButton();
        videoBtn = new javax.swing.JToggleButton();
        contactBtn = new javax.swing.JButton();
        contactBtn.setBorder(BorderFactory.createEmptyBorder());
        contactBtn.setFont(font);
        
        previewBtn = new javax.swing.JToggleButton();
        viewCombo = new javax.swing.JComboBox();
        listRendererContainer = new javax.swing.JPanel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        editMenu = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        viewMenu = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N

        jToolBar2.setRollover(true);
        jToolBar2.setName("jToolBar2"); // NOI18N

        jLabel1.setName("jLabel1"); // NOI18N
        jToolBar2.add(jLabel1);

        locationFld.setName("locationFld"); // NOI18N
        locationFld.setPreferredSize(new java.awt.Dimension(300, 25));
        locationFld.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                locationFldActionPerformed(evt);
            }
        });
        jToolBar2.add(locationFld);

        jSeparator1.setName("jSeparator1"); // NOI18N
        jToolBar2.add(jSeparator1);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(dormouse.Dormouse.class).getContext().getResourceMap(DormouseView.class);
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N
        jToolBar2.add(jLabel3);

        jLabel2.setName("jLabel2"); // NOI18N
        jToolBar2.add(jLabel2);

        searchFld.setName("searchFld"); // NOI18N
        searchFld.setPreferredSize(new java.awt.Dimension(176, 25));
        jToolBar2.add(searchFld);

        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(dormouse.Dormouse.class).getContext().getActionMap(DormouseView.class, this);
        backBtn.setAction(actionMap.get("backAction")); // NOI18N
        backBtn.setFocusable(false);
        backBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        backBtn.setName("backBtn"); // NOI18N
        backBtn.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(backBtn);

        upBtn.setAction(actionMap.get("upAction")); // NOI18N
        upBtn.setFocusable(false);
        upBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        upBtn.setName("upBtn"); // NOI18N
        upBtn.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(upBtn);

        refreshBtn.setAction(actionMap.get("refresh")); // NOI18N
        refreshBtn.setFocusable(false);
        refreshBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        refreshBtn.setName("refreshBtn"); // NOI18N
        refreshBtn.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(refreshBtn);

        bookmarksBtn.setAction(actionMap.get("showBookmarks")); // NOI18N
        bookmarksBtn.setFocusable(false);
        bookmarksBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bookmarksBtn.setName("bookmarksBtn"); // NOI18N
        bookmarksBtn.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bookmarksBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bookmarksBtnActionPerformed(evt);
            }
        });
        jToolBar1.add(bookmarksBtn);

        homeBtn.setAction(actionMap.get("showHome")); // NOI18N
        homeBtn.setFocusable(false);
        homeBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        homeBtn.setName("homeBtn"); // NOI18N
        homeBtn.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(homeBtn);

        docsBtn.setAction(actionMap.get("showDocs")); // NOI18N
        docsBtn.setFocusable(false);
        docsBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        docsBtn.setName("docsBtn"); // NOI18N
        docsBtn.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(docsBtn);

        picturesBtn.setAction(actionMap.get("showPictures")); // NOI18N
        picturesBtn.setFocusable(false);
        picturesBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        picturesBtn.setName("picturesBtn"); // NOI18N
        picturesBtn.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(picturesBtn);

        musicBtn.setAction(actionMap.get("showMusic")); // NOI18N
        musicBtn.setFocusable(false);
        musicBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        musicBtn.setName("musicBtn"); // NOI18N
        musicBtn.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(musicBtn);

        videoBtn.setAction(actionMap.get("showVideos")); // NOI18N
        videoBtn.setFocusable(false);
        videoBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        videoBtn.setName("videoBtn"); // NOI18N
        videoBtn.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(videoBtn);

        contactBtn.setAction(actionMap.get("showContacts")); // NOI18N
        contactBtn.setFocusable(false);
        contactBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        contactBtn.setName("contactBtn"); // NOI18N
        contactBtn.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(contactBtn);

        previewBtn.setAction(actionMap.get("togglePreview")); // NOI18N
        previewBtn.setText(resourceMap.getString("previewBtn.text")); // NOI18N
        previewBtn.setFocusable(false);
        previewBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        previewBtn.setName("previewBtn"); // NOI18N
        previewBtn.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        previewBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                previewBtnActionPerformed(evt);
            }
        });
        jToolBar1.add(previewBtn);

        viewCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        viewCombo.setName("viewCombo"); // NOI18N
        jToolBar1.add(viewCombo);

        listRendererContainer.setName("listRendererContainer"); // NOI18N
        listRendererContainer.setLayout(new java.awt.BorderLayout());

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 725, Short.MAX_VALUE)
            .addComponent(listRendererContainer, javax.swing.GroupLayout.DEFAULT_SIZE, 725, Short.MAX_VALUE)
            .addComponent(jToolBar2, javax.swing.GroupLayout.DEFAULT_SIZE, 725, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(listRendererContainer, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        editMenu.setText(resourceMap.getString("editMenu.text")); // NOI18N
        editMenu.setName("editMenu"); // NOI18N

        jMenuItem2.setText(resourceMap.getString("jMenuItem2.text")); // NOI18N
        jMenuItem2.setName("jMenuItem2"); // NOI18N
        editMenu.add(jMenuItem2);

        jMenuItem3.setText(resourceMap.getString("jMenuItem3.text")); // NOI18N
        jMenuItem3.setName("jMenuItem3"); // NOI18N
        editMenu.add(jMenuItem3);

        jMenuItem4.setText(resourceMap.getString("jMenuItem4.text")); // NOI18N
        jMenuItem4.setName("jMenuItem4"); // NOI18N
        editMenu.add(jMenuItem4);

        jSeparator2.setName("jSeparator2"); // NOI18N
        editMenu.add(jSeparator2);

        menuBar.add(editMenu);

        viewMenu.setText(resourceMap.getString("viewMenu.text")); // NOI18N
        viewMenu.setName("viewMenu"); // NOI18N

        jMenuItem1.setAction(actionMap.get("showBookmarks")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        viewMenu.add(jMenuItem1);

        menuBar.add(viewMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 725, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 539, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

private void previewBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_previewBtnActionPerformed
    JToggleButton src = (JToggleButton) evt.getSource();
    if (src.isSelected()) {
        showPreview(true);
    } else {
        showPreview(false);
    }
}//GEN-LAST:event_previewBtnActionPerformed

	private void bookmarksBtnActionPerformed(java.awt.event.ActionEvent evt) {// GEN
																				// -
																				// FIRST
																				// :
																				// event_bookmarksBtnActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_bookmarksBtnActionPerformed

	private void locationFldActionPerformed(java.awt.event.ActionEvent evt) {// GEN
																				// -
																				// FIRST
																				// :
																				// event_locationFldActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_locationFldActionPerformed

	@Action
	public Task upAction() {
		return new UpAction(getApplication());
	}

	@Action
	public Task backAction() {
		return new BackAction(getApplication());
	}

	@Action
	public Task showBookmarks() {
		return new ShowBookmarksTask(getApplication());
	}

	@Action
	public Task showHome() {
		return new ShowHomeTask(getApplication());
	}

	@Action
	public Task showDocs() {
		return new ShowDocsTask(getApplication());
	}

	@Action
	public Task showPictures() {
		return new ShowPicturesTask(getApplication());
	}

	@Action
	public Task showMusic() {
		return new ShowMusicTask(getApplication());
	}

	@Action
	public Task showVideos() {
		return new ShowVideoTask(getApplication());
	}

	@Action
	public Task showContacts() {
		return new ShowContactsTask(getApplication());
	}
        
        @Action
        public Task refresh(){
            return new ReloadAction(getApplication());
        }

    private class RefreshTask extends org.jdesktop.application.Task<Object, Void> {
        RefreshTask(org.jdesktop.application.Application app) {
            // Runs on the EDT.  Copy GUI state that
            // doInBackground() depends on from parameters
            // to RefreshTask fields, here.
            super(app);
        }
        @Override protected Object doInBackground() {
            // Your Task's code here.  This method runs
            // on a background thread, so don't reference
            // the Swing GUI from here.
            return null;  // return your result
        }
        @Override protected void succeeded(Object result) {
            // Runs on the EDT.  Update the GUI based on
            // the result computed by doInBackground().
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backBtn;
    private javax.swing.JToggleButton bookmarksBtn;
    private javax.swing.JButton contactBtn;
    private javax.swing.JToggleButton docsBtn;
    private javax.swing.JMenu editMenu;
    private javax.swing.JToggleButton homeBtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JPanel listRendererContainer;
    private dormouse.vfs.ui.LocationComboBox locationFld;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JToggleButton musicBtn;
    private javax.swing.JToggleButton picturesBtn;
    private javax.swing.JToggleButton previewBtn;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JButton refreshBtn;
    private javax.swing.JTextField searchFld;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JButton upBtn;
    private javax.swing.JToggleButton videoBtn;
    private javax.swing.JComboBox viewCombo;
    private javax.swing.JMenu viewMenu;
    // End of variables declaration//GEN-END:variables

	private final Timer messageTimer;

	private final Timer busyIconTimer;

	private final Icon idleIcon;

	private final Icon[] busyIcons = new Icon[15];

	private int busyIconIndex = 0;

	private JDialog aboutBox;
        
    private IFileListRenderer getSelectedRenderer(){
        return (IFileListRenderer)this.viewCombo.getSelectedItem();
    }

    private void showPreview(boolean showPreview) {
        if (!showPreview){
            this.listRendererContainer.remove(this.previewPanel);
        }else {
            IFileListRenderer renderer = this.getSelectedRenderer();
            renderer.addFileSelectionListener(new IFileSelectionListener(){

                public void fileSelected(IFileSelectionEvent event) {
                	FileObject file = event.getSelectedFile();
                    try {
                        if (file != null && file.getType() == FileType.FILE) {
                            DormouseView.this.previewPanel.preview(file);
                        }
                    } catch (FileSystemException ex) {
                        Logger.getLogger(DormouseView.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            
            });
            this.listRendererContainer.add(this.previewPanel, BorderLayout.EAST);
            
        }
        this.listRendererContainer.validate();
    }
}
